package com.ruoyi.project.hcy.notice.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.hcy.notice.domain.NoticeAndType;
import com.ruoyi.project.hcy.notice.domain.NoticeSeeCount;
import com.ruoyi.project.hcy.notice.domain.Notices;
import com.ruoyi.project.hcy.notice.mapper.NoticeAndTypeMapper;
import com.ruoyi.project.hcy.notice.service.INoticeManagerService;
import com.ruoyi.project.hcy.notice.service.INoticeSeeCountService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("notice/count")
public class NoticeSeeCountController extends BaseController {
    @Autowired
    private INoticeSeeCountService noticeSeeCountService;

    @Autowired
    private INoticeManagerService noticeManagerService;
    @Autowired
    private NoticeAndTypeMapper noticeAndTypeMapper;

    @Autowired
    private IUserService userService;

    String prefix="hcy/notice";

    /**
     * 跳转统计页面
     * @return
     */
    @RequiresPermissions("notice:look:view")
    @GetMapping()
    public String noticeSeeCount()
    {

        return prefix + "/noticeSeeCount";
    }


    @RequiresPermissions("notice:look:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(NoticeSeeCount noticeSeeCount){
        startPage();
        List<NoticeSeeCount> list = noticeSeeCountService.selectNoticeSeeCountList(noticeSeeCount);
        return getDataTable(list);
    }


    @RequiresPermissions("notice:look:look")
//    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @GetMapping("/look")
    public String look(Integer noticeId, ModelMap mmap)
    {
        System.out.println(noticeId);
        //通过id得到公告对象
        Notices notices = noticeManagerService.selectNoticesById(noticeId);
        //返回给前台显示的数据
        mmap.put("notices", notices);
        Long userId = notices.getUserId();
        User user = userService.selectUserById(userId);
        //得到发布者的名字
        mmap.put("userName", user.getLoginName());

        //每调用一次，统计表加1  记录最后一次浏览时间 判断数据库有没有这条数据，没有就插入，有就更新（id）

        NoticeSeeCount noticeSeeCount = noticeSeeCountService.selectNoticeSeeCountById(noticeId);
        //得到typeId
        NoticeAndType noticeAndType = noticeAndTypeMapper.selectNoticeAndTypeById(noticeId);
        //组装NoticeSeeCount
        NoticeSeeCount noticeSeeCount1 = new NoticeSeeCount();
        //公告id
        noticeSeeCount1.setNoticeId(noticeId);
        //类型id
        noticeSeeCount1.setTypeId(noticeAndType.getTypeId());
        //当前时间
        noticeSeeCount1.setSeeTime(new Date());
        //公告名称
        noticeSeeCount1.setNoticeName(notices.getNoticeName());
        //第一次被查看
        if(noticeSeeCount==null){
            noticeSeeCount1.setSeeCount(1l);
            noticeSeeCountService.insertNoticeSeeCount(noticeSeeCount1);
        }else{
            long a=noticeSeeCount.getSeeCount()+1;
            noticeSeeCount1.setSeeCount(a);
            noticeSeeCountService.updateNoticeSeeCount(noticeSeeCount1);
        }


        return prefix + "/noticesLook";
    }


}
